<template>
  <div class="app-container">
    <el-form inline :model="dataForm">
      <el-form-item>
        <el-input
          size="small"
          v-model="dataForm.roleName"
          placeholder="角色组名称"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="init"
          >查询</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="addOrUpdateHandle()"
          >新增角色组</el-button
        >
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="addOrUpdateHandle()"
          >新增角色</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      :data="tableData"
      row-key="id"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'flag' }"
      style="width: 100%"
      :header-cell-style="{ background: '#f2f2f2' }"
      lazy
      :load="load"
    >
      <el-table-column prop="roleName" label="角色组/角色" width="180" />
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdate" @refreshDataList="init"></add-or-update>
  </div>
</template>

<script>
import { queryAllList } from "@/api/sys-role"
import AddOrUpdate from "./AddOrUpdate"
import { mapState } from "vuex"
export default {
  components: {
    AddOrUpdate,
  },
  data() {
    return {
      dataForm: { roleName: "" },
      tableData: [
        {
          flag: true,
          id: 64,
          roleName: "角色组",
        },
      ],
      defaultProps: {
        children: "children",
        label: "roleName",
      },
    }
  },
  created() {
    this.init()
  },
  methods: {
    async init() {
      const res = await queryAllList({ appId: 88 })
      console.log(res)
      // this.tableData = res.data
    },
    load(tree, treeNode, resolve) {
      console.log(tree)
      setTimeout(() => {
        resolve([
          {
            children: [],
            flag: false,
            id: 65,
            roleName: "角色",
          },
        ])
      }, 1000)
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.$refs.addOrUpdate.init(id)
    },
  },
  computed: {
    ...mapState({
      appId: state => state.application.appId,
    }),
  },
}
</script>